

var countdown=60;
function settime(val,url,type) {
    if (countdown == 0) {
        val.removeAttribute("disabled");
        val.value="获取验证码";
        $(val).css("cursor","pointer");
        countdown = 60;
        return false;
    } else {
        if(countdown == 60){
            var phone = $('#validate_phone').val();
            if(!phone){
                layer.msg("请输入手机号码", {icon: 2, time: 1000});
                return false;
            }
            val.setAttribute("disabled", true);
            $(val).css("cursor","not-allowed");
            $.ajax({
                type: 'POST',
                url: url,
                data: {
                    phone : phone,
                    type : type,
                },
                dataType: 'json',
                beforeSend:function(){
                },
                success: function(data){
                    if(data.status){
                        layer.msg(data.info,{icon:1,time:1000});
                    }else{
                        val.removeAttribute("disabled");
                        val.value="获取验证码";
                        $(val).css("cursor","pointer");
                        countdown = 60;
                        layer.msg(data.info,{icon:2,time:1000});
                    }
                },
                error:function(data) {
                    console.log(data.msg);
                },
            });
        }
        val.value="重新发送(" + countdown + ")";
        countdown--;
    }

    setTimeout(function() {
        settime(val)
    },1000)
}

$(function() {
         // 如果不支持placeholder，用jQuery来完成
         if(!isSupportPlaceholder()) {
                 // 遍历所有input对象, 除了密码框
                 $('input').not("input[type='password']").each(
                         function() {
                             var self = $(this);
                             var val = self.attr("placeholder");
                             input(self, val);
                         }
                 );

                 $('input[type="password"]').each(
                         function() {
                             var pwdField    = $(this);
                             var pwdVal      = pwdField.attr('placeholder');
                             var pwdId       = pwdField.attr('id');
                             // 重命名该input的id为原id后跟1
                             pwdField.after('<input id="' + pwdId +'1" type="text" value='+pwdVal+' autocomplete="off" />');
                             var pwdPlaceholder = $('#' + pwdId + '1');
                             pwdPlaceholder.show();
                             pwdField.hide();

                             pwdPlaceholder.focus(function(){
                                     pwdPlaceholder.hide();
                                     pwdField.show();
                                     pwdField.focus();
                                 });

                             pwdField.blur(function(){
                                     if(pwdField.val() == '') {
                                             pwdPlaceholder.show();
                                             pwdField.hide();
                                         }
                                 });
                         }
                 );
             }
     });

 // 判断浏览器是否支持placeholder属性
 function isSupportPlaceholder() {
         var input = document.createElement('input');
         return 'placeholder' in input;
     }

 // jQuery替换placeholder的处理
 function input(obj, val) {
         var $input = obj;
         var val = val;
         $input.attr({value:val});
         $input.focus(function() {
                 if ($input.val() == val) {
                         $(this).attr({value:""});
                     }
             }).blur(function() {
                 if ($input.val() == "") {
                        $(this).attr({value:val});
                     }
             });
     }